package com.genesis.yunnanji.view;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.AddressBean;
import com.youth.xframe.adapter.XRecyclerViewAdapter;
import com.youth.xframe.adapter.XViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2018/9/25 on 9:08
 * 作者:Kite
 * 描述:收货地址
 */
public class DeliveryAddressActivity extends BaseActivity {

    @BindView(R.id.rv_address)
    RecyclerView rvAddress;

    @BindView(R.id.tv_no_data)
    TextView tvNoData;

    private int mJoinIn = 0;//0我的界面，1个人设置界面

    private AddressAdapter mAddressAdapter;//地址适配器

    public static List<AddressBean> mAddressBean;//地址Bean

    @Override
    public int getLayoutId() {
        return R.layout.activity_delivery_address;
    }

    @Override
    public void initView() {
        super.initView();
        Bundle bundle = null;
        if (bundle == null) {
            mJoinIn = getIntent().getIntExtra("p0", 0);
        }
        setTitle("收货地址");
        mAddressBean = new ArrayList<>();
        dataIsEmpty();
        mAddressAdapter = new AddressAdapter(rvAddress, mAddressBean);
        mAddressAdapter.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        rvAddress.setAdapter(mAddressAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        dataIsEmpty();
        mAddressAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.tv_add_address)
    public void onViewClicked() {
        advance(AddDeliveryAddressActivity.class, 0);
    }

    class AddressAdapter extends XRecyclerViewAdapter<AddressBean> {

        public AddressAdapter(@NonNull RecyclerView mRecyclerView, List<AddressBean> dataLists) {
            super(mRecyclerView, dataLists, R.layout.item_address);
        }

        @Override
        protected void bindData(final XViewHolder holder, final AddressBean data, int position) {
            holder.setText(R.id.tv_user_name, data.getName());
            holder.setText(R.id.tv_user_phone, data.getMobile());
            holder.setText(R.id.tv_address, data.getAddress());
            if (data.getIsDefault() == 0) {
                holder.setImageResource(R.id.iv_choose, R.drawable.icon_unchoose);
            } else {
                holder.setImageResource(R.id.iv_choose, R.drawable.icon_choose);
            }
            holder.setOnClickListener(R.id.tv_edit, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    advance(AddDeliveryAddressActivity.class, 1, data);
                }
            });
            holder.setOnClickListener(R.id.iv_choose, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (data.getIsDefault() == 0) {
                        holder.setImageResource(R.id.iv_choose, R.drawable.icon_choose);
                    } else if (data.getIsDefault() == 1) {
                        holder.setImageResource(R.id.iv_choose, R.drawable.icon_unchoose);
                    } else {
                        holder.setImageResource(R.id.iv_choose, R.drawable.icon_unchoose);
                    }
                }
            });
        }
    }

    private void dataIsEmpty() {
        if (mAddressBean.size() == 0) {
            tvNoData.setVisibility(View.VISIBLE);
            rvAddress.setVisibility(View.GONE);
        } else {
            tvNoData.setVisibility(View.GONE);
            rvAddress.setVisibility(View.VISIBLE);
        }
    }

}
